<?php
namespace App\Http\Controllers\ApiApp;

use App\DBs\AdminDB;
use Illuminate\Http\Request;

class AdminController extends BaseController
{
    /**
     *
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new AdminDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        $resData = $this->db->getAdminList();
        if ($resData['code']!=0) {
            return $this->toAppJson(-4);
        }
        $dataArr = $resData['data'];
        return $this->toAppJson(0,$dataArr);
    }

    /**
     * 新增
     */
    public function setAdd(Request $request)
    {
        $token = $request->post('token','');
        $name = $request->post('name','');
        $genre = $request->post('genre','');
        if (!$name || !$genre) {
            return $this->toAppJson(-3);
        }
        $resDb = $this->db->setAdminAdd($name,$genre);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5);
        }
        return $this->toAppJson(0);
    }

    /**
     * 通过TOKEN，获取管理员信息
     */
    public function getInfoByToken(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-3);
        }
        $resData = $this->db->getAdminList();
        if ($resData['code']!=0) {
            return $this->toAppJson(-4);
        }
        $dataArr = $resData['data'];
        return $this->toAppJson(0,$dataArr);
    }

    /**
     * 通过ADMINID，获取管理员信息
     */
    public function getOne(Request $request)
    {
        $token = $request->post('token','');
        $adminType = $this->getAdminTypeByToken($token);
        if (!in_array($adminType,[1,2])) {
            return $this->toAppJson(-3);
        }
        $adminId = $request->post('adminId',0);
        $resData = $this->db->getAdminOne($adminId);
        if ($resData['code']!=0) {
            return $this->toAppJson(-4);
        }
        $dataArr = $resData['data'];
        return $this->toAppJson(0,$dataArr);
    }

    /**
     * 更新用户名
     */
    public function setNickname(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-3);
        }
        $name = $request->post('name','');
        if (!$name) {
            return $this->toAppJson(-3);
        }
        $resDb = $this->db->setNickname($adminId,$name);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-9);
        }
        return $this->toAppJson(0);
    }

    /**
     * 更新
     */
    public function setUpdate(Request $request)
    {
        $token = $request->post('token','');
        $adminType = $this->getAdminTypeByToken($token);
        if (!in_array($adminType,[1,2])) {
            return $this->toAppJson(-2);
        }
        $adminId = $request->post('adminId','');
        $name = $request->post('name','');
        $genre = $request->post('genre','');
        $pwd = $request->post('pwd','');
        $genres = $this->db->getGenres();
        if (!$adminId || !$name || !array_key_exists($genre,$genres)) {
            return $this->toAppJson(-3);
        }
        $resOne = $this->db->getOne($adminId);
        if ($resOne['code']!=0) {
            return $this->toAppJson(-4);
        }
        $adminOne = $resOne['data'];
        if ($pwd && !preg_match('/[0-9a-zA-Z]{6,20}/',md5($pwd))) {
            return $this->toAppJson(-10);
        }
        $password = $pwd ? md5($pwd) : $adminOne['password'];
        $resDb = $this->db->setUpdate($adminId,$name,$password,$genre);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-9);
        }
        return $this->toAppJson(0);
    }

    /**
     * 更新密码
     */
    public function setPwdUpdate(Request $request)
    {
        $token = $request->post('token','');
        $adminId = $this->getAdminByToken($token);
        if (!$adminId) {
            return $this->toAppJson(-3);
        }
        $pwd = $request->post('pwd','');
        $pwd2 = $request->post('pwd2','');
        $resOne = $this->db->getOne($adminId);
        if ($resOne['code']!=0) {
            return $this->toAppJson(-4);
        }
        $adminOne = $resOne['data'];
        $password = md5($pwd);
        $password2 = md5($pwd2);
        if (!preg_match('/[0-9a-zA-Z]{6,20}/',$password)) {
            return $this->toAppJson(-10);
        }
        if (!preg_match('/[0-9a-zA-Z]{6,20}/',$password2)) {
            return $this->toAppJson(-10);
        }
        if ($password!=$adminOne['password']) {
            return $this->toAppJson(-8);
        }
        $name = $adminOne['adminName'];
        $genre = $adminOne['genre'];
        $resDb = $this->db->setUpdate($adminId,$name,$password2,$genre);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-9);
        }
        return $this->toAppJson(0);
    }

//    /**
//     * 更新头像
//     */
//    public function setHeadUpdate(Request $request)
//    {
//        $token = $request->post('token','');
//        $name = $request->post('name','');
//    }

    /**
     * 登陆
     */
    public function setLogin(Request $request)
    {
        $name = $request->post('name','');
        $pwd = $request->post('pwd','');
//        return $this->toAppJson(-1,[$name,$pwd]);
        if (!$name || !$pwd) {
            return $this->toAppJson(-3);
        }
        $adminOne = $this->db->getAdminByName($name,$pwd);
        if ($adminOne['code']!=0) {
            return $this->toAppJson(-7);
        }
        $adminId = $adminOne['data']['adminId'];
        $token = $this->setTokenByAdminid($adminId);
        if (!$token) {
            return $this->toAppJson(-6);
        }
        $dataArr = [
            'token' => $token,
            'adminInfo' => $adminOne['data'],
        ];
        return $this->toAppJson(0,$dataArr);
    }
}
